//
//  ByUIPswLoginVC.m
//  oc_knowledge
//
//  Created by justiOS on 17/1/16.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByUIPswLoginVC.h"

#import "ByTabBarController.h"
// net
#import "ByApiUser.h"

#import "ByCacheUser.h"

@interface ByUIPswLoginVC ()<UITextFieldDelegate>
@property (nonatomic, strong) UITextField *mobleTF;
@property (nonatomic, strong) UITextField *pswTF;
@property (nonatomic, strong) UIButton *loginButton;
@property (nonatomic, strong) UIButton *seeButton;
@property (nonatomic, strong) UIButton *deleButton1;
@property (nonatomic, strong) UIButton *deleButton2;
@end

@implementation ByUIPswLoginVC
#pragma mark getter

- (UITextField *)mobleTF {
    if (!_mobleTF) {
        _mobleTF = [[UITextField alloc] init];
        _mobleTF.placeholder = @"请输入手机号";
        _mobleTF.font = FONT(14);
        _mobleTF.tintColor = BY_THEME_COLOR;
        _mobleTF.leftViewMode = UITextFieldViewModeAlways;
        UILabel *leftLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60, 40)];
        leftLabel.font = FONT(14);
        leftLabel.text = @"账号";
        leftLabel.textAlignment = NSTextAlignmentCenter;
        _mobleTF.leftView = leftLabel;
        _mobleTF.delegate = self;
        [_mobleTF addTarget:self action:@selector(mobleTFChange:) forControlEvents:UIControlEventEditingChanged];
    }
    return _mobleTF;
}

- (UITextField *)pswTF {
    if (!_pswTF) {
        _pswTF = [[UITextField alloc] init];
        _pswTF.placeholder = @"请输入密码";
        _pswTF.tintColor = BY_THEME_COLOR;
        _pswTF.font = FONT(14);
        _pswTF.leftViewMode = UITextFieldViewModeAlways;
        UILabel *leftLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60, 40)];
        leftLabel.font = FONT(14);
        leftLabel.textAlignment = NSTextAlignmentCenter;
        leftLabel.text = @"密码";
        _pswTF.leftView = leftLabel;
        _pswTF.delegate = self;
        _pswTF.secureTextEntry = YES;
        [_pswTF addTarget:self action:@selector(mobleTFChange:) forControlEvents:UIControlEventEditingChanged];
        
    }
    return _pswTF;
}

- (UIButton *)deleButton1 {
    if (!_deleButton1) {
        _deleButton1 = [UIButton buttonWithType:UIButtonTypeCustom];
        [_deleButton1 setImage:[UIImage imageNamed:@"Register_delete"] forState:UIControlStateNormal];
        [_deleButton1 addTarget:self action:@selector(deleAction1:) forControlEvents:UIControlEventTouchUpInside];
        _deleButton1.hidden = YES;
    }
    return _deleButton1;
}

- (UIButton *)deleButton2 {
    if (!_deleButton2) {
        _deleButton2 = [UIButton buttonWithType:UIButtonTypeCustom];
        [_deleButton2 setImage:[UIImage imageNamed:@"Register_delete"] forState:UIControlStateNormal];
        [_deleButton2 addTarget:self action:@selector(deleAction2:) forControlEvents:UIControlEventTouchUpInside];
        _deleButton2.hidden = YES;
    }
    return _deleButton2;
}


- (UIButton *)seeButton {
    if (!_seeButton) {
        _seeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_seeButton setImage:[UIImage imageNamed:@"Register_eyec"] forState:UIControlStateNormal];
        [_seeButton setImage:[UIImage imageNamed:@"Register_eyeo"] forState:UIControlStateSelected];
        [_seeButton addTarget:self action:@selector(changeSecureStatue:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _seeButton;
}


- (UIButton *)loginButton {
    if (!_loginButton) {
        _loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _loginButton.layer.cornerRadius = 3;
        _loginButton.backgroundColor = BY_THEME_COLOR;
        [_loginButton setTitle:@"登录" forState:UIControlStateNormal];
        [_loginButton addTarget:self action:@selector(loginAction:) forControlEvents:UIControlEventTouchUpInside];

    }
    return _loginButton;
}



#pragma mark UI
- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatViews];
    self.view.userInteractionEnabled = true;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapEmptyAction:)];
    [self.view addGestureRecognizer:tap];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(gettext:) name:@"registermodel" object:nil];
}
- (void)gettext:(NSNotification *)text {
    NSDictionary *dic = text.userInfo;
    self.mobleTF.text = dic[@"phone"];
    self.pswTF.text = dic[@"psw"];
}
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"registermodel" object:nil];
}
- (void)creatViews {
    self.mobleTF.frame = CGRectMake(0, 0, SCREEN_WIDTH - 30, 40);
    self.pswTF.frame = CGRectMake(0, 40, SCREEN_WIDTH - 55, 40);
    [self.view addSubview:self.mobleTF];
    [self.view addSubview:self.pswTF];
    
    self.deleButton1.frame = CGRectMake(SCREEN_WIDTH - 30, 10, 20, 20);
    self.deleButton2.frame = CGRectMake(SCREEN_WIDTH - 55, 50, 20, 20);
    [self.view addSubview:self.deleButton1];
    [self.view addSubview:self.deleButton2];
    
    self.seeButton.frame = CGRectMake(SCREEN_WIDTH - 30, 50, 20, 20);
    [self.view addSubview:self.seeButton];
    
    
    UIView *line1 = [[UIView alloc] initWithFrame:CGRectMake(10, 40, SCREEN_WIDTH - 10, 0.5)];
    line1.backgroundColor = BY_LINE_COLOR;
    [self.view addSubview:line1];
    
    UIView *line2 = [[UIView alloc] initWithFrame:CGRectMake(0, 80, SCREEN_WIDTH, 0.5)];
    line2.backgroundColor = BY_LINE_COLOR;
    [self.view addSubview:line2];

    self.loginButton.frame = CGRectMake(10, line2.bottom + 20, SCREEN_WIDTH - 20, 40);
    [self.view addSubview:self.loginButton];

}



#pragma mark 点击事件
- (void)tapEmptyAction:(UITapGestureRecognizer *)tap {
    
    [UIView animateWithDuration:0.4 animations:^{
        [self.pswTF resignFirstResponder];
        [self.mobleTF resignFirstResponder];
    }];
}

- (void)deleAction1:(UIButton *)button {
    self.mobleTF.text = nil;
    button.hidden = YES;
}

- (void)deleAction2:(UIButton *)button {
    self.pswTF.text = nil;
    button.hidden = YES;
}

- (void)changeSecureStatue:(UIButton *)button {
    button.selected = !button.selected;
    _pswTF.secureTextEntry = !_pswTF.secureTextEntry;
}

- (void)loginAction:(UIButton *)button {

    if ([self.mobleTF.text isStringNull]) {
        [ByCoreToolAlert showOnlyText:@"手机号不能为空"];
        return;
    }
    if ([self.pswTF.text isStringNull]) {
        [ByCoreToolAlert showOnlyText:@"密码不能为空"];
        return;
    }
    if (self.mobleTF.text.length != 11) {
        [ByCoreToolAlert showOnlyText:@"手机号输入不正确"];
        return;
    }
    ByNetParamsModel *model = [[ByNetParamsModel alloc] init];
    model.username = self.mobleTF.text;
    model.password = self.pswTF.text;
    [ByCoreToolAlert showLoadind:@"登录中..." hasMasker:YES];
    [ByApiUser login:model success:^(ByNetBaseResult *result) {
        [ByCoreToolAlert hideHud];
        ByModelUser *model = (ByModelUser *)result.data;
        // 缓存用户model
        [ByCacheUser setUserInfo:model];
        [self dismissViewControllerAnimated:YES completion:^{
            ByTabBarController *tab = (ByTabBarController *)((AppDelegate *)BY_APPDELEGATE).window.rootViewController;
            tab.selectedIndex = _seletedTag;
        }];
        
    } failure:^(ByNetBaseResult *result) {
        [ByCoreToolAlert showFailureWithText:result.msg];
    }];

}


#pragma mark tf delegate
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    if (textField == self.mobleTF) {
        if (textField.text.length > 0) {
            self.deleButton1.hidden = NO;
        }else{
            self.deleButton1.hidden = YES;
        }
    }else{
        if (textField.text.length > 0) {
            self.deleButton2.hidden = NO;
        }else{
            self.deleButton2.hidden = YES;
        }
    }
    
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    if (textField == self.mobleTF) {
        self.deleButton1.hidden = YES;
    }else{
        self.deleButton2.hidden = YES;
    }
}

- (void)mobleTFChange:(UITextField *)textField {
    if (textField == self.mobleTF) {
        if (textField.text.length > 0) {
            self.deleButton1.hidden = NO;
        }else{
            self.deleButton1.hidden = YES;
        }
        if (textField.text.length > 11) {
            textField.text = [textField.text substringToIndex:11];
        }
    }else {
        if (textField.text.length > 0) {
            self.deleButton2.hidden = NO;
        }else{
            self.deleButton2.hidden = YES;
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
